package com.javasm.sale;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.util.Constants;
import com.javasm.sale.service.CommodityOrderService;
import com.javasm.sale.service.OrderService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("order")
public class OrderController extends BaseController {
    @Resource
    private OrderService orderService;
    @Resource
    private CommodityOrderService commodityOrderService;

    @GetMapping("{id}")
    public AxiosResult getByid(@PathVariable String id) {
        Order obj = this.orderService.getById(id);
        return suc(obj);
    }

    @GetMapping("/orderGoods/{id}")
    public AxiosResult getOrderAndListGoodsByid(@PathVariable String id) {
        Order byId = orderService.getById(id);
        List<CommodityOrder> list = commodityOrderService.listGoodsByOrderId(id);
        byId.setCommodityList(list);
        return suc(byId);
    }


    @GetMapping("list")
    public TableDatas listOrderAndCommodity(Order order){
        startPage();
        List<Order> orders = orderService.listOrder(order);
        return toTableDatas(orders);
    }

    @GetMapping("imeaList")
    public TableDatas listOrderAndIMEA(Map<String,String> map){
        startPage();
        List<Order> orders = orderService.listOrderAndIMEA(map);
        return toTableDatas(orders);
    }

    @GetMapping("excelDownload")
    public ResponseEntity excelDownload( String objectName ,String relName) throws Exception {
        if(StringUtils.isEmpty(objectName)) objectName = Constants.ORDER_EXCEL_TEMPLATE;
        byte[] bytes = orderService.excelTemplate(objectName);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(relName, "UTF-8"));
        return new ResponseEntity(bytes, headers, HttpStatus.OK);
    }


    @PostMapping("uploadExcel")
    public AxiosResult doFileUpload(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();//文件内容,一般对于10M以上大文件.
        Map<String, Object> stringObjectMap = orderService.readExcel(inputStream);
        return suc(stringObjectMap);
    }



    @GetMapping("queryExcelDownload")
    public ResponseEntity queryExcelDownload(Order order) throws Exception {
        startPage();
        List<Order> orders = orderService.listOrder(order);
        byte[] bytes = orderService.queryListToExcel(orders);
        HttpHeaders headers = new HttpHeaders();
        String fileName = Constants.QUERY_ORDER_FILE_NAME;
        headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        return new ResponseEntity(bytes, headers, HttpStatus.OK);
    }

    @GetMapping
    public TableDatas list(Order obj) {
        startPage();
        List<Order> list = this.orderService.list(new QueryWrapper(obj));
        return toTableDatas(list);
    }


    @PostMapping
    public AxiosResult add(@RequestBody Order order) {
        boolean b = this.orderService.save(order);
        return isok(b);
    }

    @PutMapping
    public AxiosResult updateById(@RequestBody Order order) {
        boolean b = this.orderService.updateById(order);
        return isok(b);
    }

    @DeleteMapping("{ids}")
    public AxiosResult delByIds(@PathVariable String ids) {
        String[] idsArray = ids.split(",");
        boolean b = this.orderService.removeByIds(Arrays.asList(idsArray));
        return isok(b);
    }
}

